package com.hxt.framework.api;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.BooleanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hxt.framework.dubbo.DubboIn;
import com.hxt.framework.dubbo.DubboOut;
import com.hxt.framework.dubbo.IDubboService;
import com.hxt.framework.msg.RequestMsg;
import com.hxt.framework.utils.DateUtil;
import com.hxt.framework.utils.JsonUtil;
import com.hxt.util.TypeConvert;
import com.hxt.xt.service.z.rule.CommonRule;

@SuppressWarnings({ "unchecked", "rawtypes" })
@Controller
@RequestMapping("/zbak")
public class CommonApi {

	@Resource
	IDubboService iDubboService;

	@Resource
	CommonRule comRule;

	/**
	 * 公共接口入口，反射调用
	 * 
	 * @Title: common
	 * @Description: TODO
	 * @param param
	 * @return
	 * @return: Json
	 */

	@RequestMapping(value = "{txId}")
	@ResponseBody
	public Map<String, Object> common(HttpServletRequest request, @PathVariable("txId") String txId,
			@RequestParam(value = "p", required = false) String p) {

		Map<String, Object> resp = Maps.newLinkedHashMap();
		DubboIn dubboIn = new DubboIn();
		DubboOut dubboOut = new DubboOut();
		// 接口调用
		try {

			List<Map<String, Object>> list=Lists.newArrayList();
			//登录是不需要鉴权的
//			if(BooleanUtils.negate("com10001".equals(txId)))
//			{
//				// token验证 begin
//				Map<String, String[]> m = request.getParameterMap();
//				String param = m.get("p")[0];
//				
//				RequestMsg msg = JsonUtil.deserializeRequstVo(param, RequestMsg.class);
//				
//					 list = comRule.getUserByToken(msg.getAccessToken());
//					 if (ObjectUtils.isEmpty(list)) {
//							resp.put("retCode", "300");
//							resp.put("retMsg", "处理成功");
//							resp.put("errMsg", null);
//							resp.put("reqSeq", DateUtil.getToday());
//							resp.put("result", "鉴权失败");
//							return resp;
//						}
//				}
				
				// token验证 end
				dubboIn.setTxId(TypeConvert.convertName(txId));
				dubboIn.setParam(p);
				String ip = getIpAddr(request);
				dubboIn.setIp(ip);

				dubboOut = iDubboService.doPost(dubboIn);
				resp = JsonUtil.deserializeRequstVo(dubboOut.getParam(), Map.class);
			

		} catch (Exception e) {
			e.printStackTrace();
		}
		return resp;
	}

	/**
	 * 获取当前网络ip
	 * 
	 * @param request
	 * @return
	 */
	public String getIpAddr(HttpServletRequest request) {
		String ipAddress = request.getHeader("x-forwarded-for");
		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getHeader("Proxy-Client-IP");
		}
		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getRemoteAddr();
			if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
				// 根据网卡取本机配置的IP
				InetAddress inet = null;
				try {
					inet = InetAddress.getLocalHost();
				} catch (UnknownHostException e) {
					e.printStackTrace();
				}
				ipAddress = inet.getHostAddress();
			}
		}
		// 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
		if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
															// = 15
			if (ipAddress.indexOf(",") > 0) {
				ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
			}
		}
		return ipAddress;
	}

}
